package stack;

import java.util.Stack;

/**
 * @author lakersUp
 * @Classname       逆波兰表达式
 * @Description
 * @Date 2022/4/6 9:51
 * @Version 1.0
 */
public class EvalRPN150 {
    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();
        int n=tokens.length;
        int a=0;
        while (a<n){
            if(tokens[a].equals("+") || tokens[a].equals("-") || tokens[a].equals("*") || tokens[a].equals("/")) {
                int y = stack.pop();
                int x = stack.pop();
                switch (tokens[a]){
                    case "+":
                        stack.push(x+y);
                        break;
                    case "-":
                        stack.push(x-y);
                        break;
                    case "*":
                        stack.push(x*y);
                        break;
                    case "/":
                        stack.push(x/y);
                        break;
                    default:
                }
            }else {
                stack.push(Integer.parseInt(tokens[a]));
            }
            a++;
        }
        return stack.pop();
    }
}
